$(document).ready(function(){
	var page = 1;
	var rows = 10;
	loadComment(page,rows,"","");
    $(function(){
        $(".search-box input").focus(function(){
            $(this).parent('div').css('border','1px solid #F5C63E');
        }).blur(function(){
            $(this).parent('div').css('border','1px solid #C0C0C0');
        });
    });
    $(".status").on("click",function(){
        $(this).addClass("on").siblings().removeClass("on");
        
    });
    //搜索商品
    $('.search-box span').click(function(){
    	getCondition();
    });
    //不同状态
    $(".state li").click(function(){
        $(this).addClass('active').siblings('li').removeClass('active');
        getCondition();
    });
    //
    $('.product-list tbody tr').each(function(){
    	if($(this).children('td:nth-child(10)').children('span').html()!='0.00'){
    		$(this).children('td:nth-child(11)').children('button.delete').remove();
    	}
    });
//    新增商品
    $('.add').click(function(){ 
    	window.location.href = path + "/product/product/goProductAdd";
    });
//  导入
    $('.daoru').click(function(){
    	window.location.href = path + "/product/product/goProductImport";
    });
//  发布
    $('.fabu').click(function(){
    	var idList = [];
    	var idStr = idList.join(',');
	   	$('.product-list table tbody tr').each(function(){
	   		if($(this).find('s').hasClass('selected')){
	   			idList.push($(this).attr('data-id'));
	   		}
	   	});
	   	if(idList.length == 0){
	   		promtp("请选择要发布的商品");
	   	}else{
	   		var idStr = idList.join(',');
	   		console.log(idStr);
	   		layer.confirm('是否确认发布选中商品？', {icon: 3, title:'提示'}, function(index){
	   			$.ajax({
	   	    		type:"POST",
	   	    		url: path +"/product/product/releaseProduct",
	   	    		data:{id:idStr},
	   	    		success:function(data){
	   	    			if(data.state == 1){
	   		    			layer.msg('商品发布成功',{
	   			    			  time: 1500,
	   			    			  area: '300px',
	   			    			  icon:1,
	   			    			  offset: '0'
	   			    			},function(){
	   			    				location.reload(true);
	   			    			});
	   	    			}
	   	    		},
	   	    		error:function(data){
	   	    		}
	   	    	});
	   				layer.close(index);
	   			});
	   	}
    });
    // 删除
    $('.delete-more').click(function(){
    	var idList = [];
    	var idStr = idList.join(',');
	   	$('.product-list table tbody tr').each(function(){
	   		if($(this).find('s').hasClass('selected')){
	   			idList.push($(this).attr('data-id'));
	   		}
	   	});
	   	if(idList.length == 0){
	   		promtp("请选择要删除的商品");
	   	}else{
	   		var idStr = idList.join(',');
	   		console.log(idStr);
	   		layer.confirm('是否确认删除选中商品？', {icon: 3, title:'提示'}, function(index){
	   			$.ajax({
	   	    		type:"POST",
	   	    		url: path +"/product/product/delete",
	   	    		data:{id:idStr},
	   	    		success:function(data){
	   	    			if(data.state == 1){
	   		    			layer.msg('商品删除成功',{
	   			    			  time: 1500,
	   			    			  area: '300px',
	   			    			  icon:1,
	   			    			  offset: '0'
	   			    			},function(){
	   			    				location.reload(true);
	   			    			});
	   	    			}
	   	    		},
	   	    		error:function(data){
	   	    		}
	   	    	});
	   				layer.close(index);
	   			});
	   	}
    });
  //商品分类
    $('.categoryList').on('click','s',function(e){
    	e.stopPropagation();
    	$(this).toggleClass('on');
    	$(this).siblings('ul').css('display',$(this).siblings('ul').css('display') == 'none'?'block':'none');
    });
    $(".product-type div.fl").click(function(e){
        e.stopPropagation();
        var div=$(this);
        div.children('ul').toggleClass('hide');
    });
    $('.categoryList').on('click','b',function(){
    	if($(this).parent('li').hasClass('last-type')){
    		$('.product-type').find('.show').val($(this).html());
    		$('.product-type').find('.hidden').val($(this).parent('li').attr('data-id'));
    		getCondition();
    	}else if($(this).parent('li').hasClass('all')){
    		$('.product-type').find('.show').val($(this).html());
    		$('.product-type').find('.hidden').val("");
    	}else{
    		layer.msg('请选择子分类', {
    			  time: 1500,
    			  area: '300px',
    			  icon:2,
    			  offset: '0'
    			});
    	}
    });
    function ff(){
    	$.ajax({
            type:'post',
            url:'/diconet/productCategory/findAllProdCategory',
            success:function(data){
            	var list = data.data;
            	var level1 = "";
            	for(var i=0;i<list.length;i++){
            		if(list[i].parentId == ""){
            			var isParent = 0;
            			for(var j=0;j<list.length;j++){
//            				判断该分类是否含有子分类
            				if(list[i].id == list[j].parentId){
            					isParent = 1;
            				}
            			}
            			if(isParent == 0){
            				level1 += "<li class='last-type' data-id='"+list[i].id+"'><b>"+list[i].categoryName+"</b><ul></ul></li>";
            			}else if(isParent == 1){
            				level1 += "<li data-id='"+list[i].id+"'><s>▼</s><b>"+list[i].categoryName+"</b><ul></ul></li>";
            			}
            		}
            	}
            	$('.categoryList').find('.all').after(level1);
            	$('.categoryList>li').each(function(){
            		var level2 = "";
            		var id = $(this).attr('data-id');
            		for(var i=0;i<list.length;i++){
                		if(list[i].parentId == id){
                			var isParent = 0;
                			for(var j=0;j<list.length;j++){
//                				判断该分类是否含有子分类
                				if(list[i].id == list[j].parentId){
                					isParent = 1;
                				}
                			}
                			if(isParent == 0){
                				level2 += "<li class='last-type' data-id='"+list[i].id+"'><b>"+list[i].categoryName+"</b><ul></ul></li>";
                			}else if(isParent == 1){
                				level2 += "<li data-id='"+list[i].id+"'><s>▼</s><b>"+list[i].categoryName+"</b><ul></ul></li>";
                			}
                		}
                	};
                	$(this).children('ul').html(level2);
            	});
            	$('.categoryList>li>ul>li').each(function(){
            		var level3 = "";
            		var id = $(this).attr('data-id');
            		for(var i=0;i<list.length;i++){
                		if(list[i].parentId == id){
                			level3 += "<li data-id='"+list[i].id+"' class='last-type'><b>"+list[i].categoryName+"</b><ul></ul></li>";
                		}
                	};
                	$(this).children('ul').html(level3);
            	})
            },
            error:function(){}
        });
    }
    ff();
  //获取所有查询条件
    function getCondition(){
    	var productName = $('#productName').val();
    	var isRelease = $('.state').find('li.active').attr('data-state');
    	loadComment(page,rows,productName,isRelease);
    };
//    详情、修改、删除
    $('#productList').on('click','button',function(){
    	var id = $(this).parent().parent('tr').attr('data-id');
    	if($(this).html() === '详情'){
    		productDetail(id);
    	}else if($(this).html() === '修改'){
    		productEdit(id);
    	}else if($(this).html() === '删除'){
    		productDelete(id);
    	}
    })
});
// 加载商品列表数据
//页面加载获取数据并分页
function loadComment(page,rows,productName,isRelease){
	$.ajax({
		type: "post",
		url: path + "/product/product/productList",
		data: {
			page : page,
			rows : rows,
			productName:productName,
			isRelease:isRelease,
		},
		dataType: 'json',
		success: function(data){
			var productList = data.state == 1?data.data:data.state;
			var commentContent = '';
			if(productList) {
				for(var i=0; i<productList.rows.length; i++) {
					var comment = productList.rows[i];
					var isReleaseView = comment.isRelease == 1?"是":"否";
					commentContent += '<tr data-id="'+comment.id+'">';
					commentContent += '<td><s></s></td>';
					commentContent += '<td><img src=/docs/'+comment.firstPicture+'></td>';
					commentContent += '<td>'+comment.productName+'</td>';
					commentContent += '<td>'+comment.productCode+'</td>';
					commentContent += '<td>'+comment.brandName+'</td>';
					commentContent += '<td>'+isReleaseView+'</td>';
					commentContent += '<td>'+comment.categoryName+'</td>';
					commentContent += '<td>';
					//commentContent += '<button class="detail">详情</button>';
					commentContent += '<button class="revise">修改</button>';
					commentContent += '<button class="delete">删除</button>';
					commentContent += '</td>';
					commentContent += '</tr>'
				}
				$('#productList').html(commentContent);
				var dataPage = productList.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+',\''+productName+'\',\''+isRelease+'\');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+',\''+productName+'\',\''+isRelease+'\');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+',\''+productName+'\',\''+isRelease+'\');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+',\''+productName+'\',\''+isRelease+'\');">尾页</a>';
				}
			}else{
				$('#productList').html('');
				$(".quotes").html('');
			}
			$(".quotes").html(pageContent);
		},
	});
}
function f1(){
	var state=$('.state ul').attr('data-id');
	if(state==''){
		$('.state ul li').each(function(){
			if($(this).attr('data-state')=='0'){
				$(this).addClass('active').siblings('li').removeClass('active');
			}
		})
	}else{
		$('.state ul li').each(function(){
			if($(this).attr('data-state')==state){
				$(this).addClass('active').siblings('li').removeClass('active');
			}
		})
	}
}
f1();
/**
 * 商品详情
 */
function productDetail(id){
	window.location.href = "/DistributorSystem/product/findProductInfo?id="+id;
}

/**
 * 删除商品
 */
function productDelete(id){
	var idList = [];
	idList.push(id);
	var idStr = idList.join(',');
	console.log(idList);
	console.log(idStr);
	layer.confirm('是否确认删除该商品？', {icon: 3, title:'提示'}, function(index){
		$.ajax({
    		type:"POST",
    		url: path +"/product/product/delete",
    		data:{id:idStr},
    		success:function(data){
    			if(data.state == 1){
	    			layer.msg('商品删除成功',{
		    			  time: 1500,
		    			  area: '300px',
		    			  icon:1,
		    			  offset: '0'
		    			},function(){
		    				window.location.reload(true);
		    			});
    			}
    		},
    		error:function(data){
    		}
    	});
			layer.close(index);
		});
}
function productEdit(id){
	window.location.href = path + "/product/product/goProductUpdate?productId="+id;
}

